<template>
  <el-dialog
    title="加载进度条..."
    custom-class="progress-bar"
    width="500px"
    :visible.sync="visibleDialog"
    :before-close="handleClose"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <span>下载进度:</span>
    <el-progress :percentage="percentage" :color="customColor"></el-progress>
    <span slot="footer" class="dialog-footer">
      <el-button @click="startHandle">开 始</el-button>
      <el-button @click="successHandle('立即')">立即</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    value: {
      type: Boolean,
    },
    successHandle: {
      type: Function,
    },
    errorHandle: {
      type: Function,
    },
    api: {
      type: Function,
    },
    params: {
      type: Object,
    },
  },
  data() {
    return {
      percentage: 0,
      customColor: "#409eff",
      visibleDialog: true,
    }
  },
  computed: {
    // visibleDialog: {
    //   get() {
    //     console.log("get")
    //     return this.value
    //   },
    //   set(v) {
    //     this.$emit("input", v)
    //   },
    // },
  },
  methods: {
    handleClose() {
      this.visibleDialog = false
    },
    startHandle() {
      const timer = setInterval(async () => {
        let { percentage } = await this.api(this.params)
        this.percentage = percentage
        if (percentage === 100) {
          clearInterval(timer)
          this.visibleDialog = false
          this.successHandle("xxxx")
          this.$destroy()
        }
      }, 100)
    },
  },
  created() {},
}
</script>

<style lang="less">
.progress-bar {
  font-size: normal;
}
</style>
